<template>
  <div>
    <el-card>
      <el-form :model="form" size="mini" label-position="top" ref="form">
        <el-row :gutter="10">
          <el-col :lg="4" :md="6">
            <el-form-item label="活动类型编号" prop="actId">
              <el-input v-model="form.actId" placeholder="请输入活动类型编号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6">
            <el-form-item label="活动类型名称" prop="actName">
              <el-input v-model="form.actName" placeholder="请输入活动类型名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6">
            <el-form-item label="活动大类" prop="actType">
              <el-select v-model="form.actType" clearable placeholder="请选择活动大类">
                <el-option v-for="item in dataOptions" :key="item.FIELDKEY" :label="item.FIELDVALUE"
                  :value="item.FIELDKEY"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg='8' :md='12' class="searchBtnMarginTop">
            <el-button type="primary" icon="el-icon-search" @click="actQuery" size='mini'>查询
            </el-button>
          <!-- </el-col>
          <el-col :lg="2" :md="3" class="searchBtnMarginTop"> -->
            <el-button type="warning" size="mini" icon="el-icon-refresh" @click="resetForm('form')">重置</el-button>
          <!-- </el-col> -->
          <!-- <el-col :lg="2" :md="3" v-if="btnPermission('Button_User_Add')" class="searchBtnMarginTop">
            <el-button type="primary" size="mini" @click="createUser" icon='el-icon-plus'>新增用户</el-button>
          </el-col> -->
          <!-- <el-col :lg="2" :md="3" class="searchBtnMarginTop"> -->
            <el-button type="primary" size="mini" icon="el-icon-plus" @click="addAct">新增活动类型</el-button>
          </el-col>
        </el-row>
      </el-form>
      <div>
        <el-table :data="tableData">
          <el-table-column prop="ACTIVITYTYPE" label="活动类型编号"></el-table-column>
          <el-table-column prop="ACTIVITYTYPENAME" label="活动类型名称"></el-table-column>
          <el-table-column prop="ACTIVITYCATEG" label="活动大类" align="center">
            <template slot-scope="scope">
              <div>
                {{scope.row.ACTIVITYCATEG | formatAct}}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="ISPAID" label="是否带薪" align="center">
            <template slot-scope="scope">
              <div>
                {{scope.row.ISPAID === "1"?"是":"否"}}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="日历颜色" align="center">
            <template slot-scope="scope">
              <div class="calendarColor" :style="'background:'+scope.row.CALCOLOUR+';'"></div>
            </template>
          </el-table-column>
          <el-table-column prop="ISENABLEHOLIDAY" label="是否休假" align="center">
            <template slot-scope="scope">
              <div>
                {{scope.row.ISENABLEHOLIDAY === "1"?"是":"否"}}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="NOTES" label="说明"></el-table-column>
          <el-table-column label="操作" fixed="right" width="100">
            <template slot-scope="scope">
              <el-button @click="update(scope.row)" type="text" size="small">修改</el-button>
              <el-button type="text" size="small" @click="delAct(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-col>
            <el-pagination style="text-align: right; margin-top:20px;" :page-sizes="[10, 20, 50, 100]"
              :page-size="pageSize" @size-change="handleInSizeChange" @current-change="handleInCurrentChange"
              :current-page="currpage" background layout="sizes, total, prev, pager, next, jumper" :total="tableTotal">
            </el-pagination>
          </el-col>
        </el-row>
      </div>
      <component :is="dialogList.editDialog" :option="editOptions" @ListenAct="actQuery"></component>

    </el-card>
  </div>
</template>
<script>
  import editDialog from './dialog/editDialog'
  import request from "@/common/utils/requestWFM";
  import Query from '@/message/wfm/scheduleDataManage/activityManage/actQuery'
  import Del from '@/message/wfm/scheduleDataManage/activityManage/actDel'
  import getGlobalParams from "@/common/utils/getGlobalParams";
  import optionQuery from '@/message/wfm/baseData/dictionaryManage/dictionaryQuery'

  let that
  export default {
    beforeCreate: function () {
      that = this;
    },
    filters: {
      formatAct: (val) => {
        // console.log(that.dataOptions)
        let str = ''
        that.dataOptions.forEach(item => {
          if (val == item.FIELDKEY) {
            str = item.FIELDVALUE
          }
        });
        return str
      }
    },
    data() {
      return {
        dataOptions: [], //下拉列表
        tableTotal: 1,
        currpage: 1,
        pageSize: getGlobalParams.get("PageSize"),
        form: { //表单数据
          actId: "",
          actName: "",
          actType: "",
        },
        actId: '',
        editOptions: {
          isShow: false,
          title: '',
        },
        dialogList: {
          editDialog
        },
        tableData: []
      }
    },
    created() {
      this.queryOption()
    },
    mounted() {
      this.actQuery()
      // this.queryOption()
    },
    methods: {
      // 下拉框查询
      async queryOption() {
        let resBody = new optionQuery();
        resBody.data.FIELDCODE = 'ACTIVITYCATEG';
        resBody.data.CurrPage = 1
        resBody.data.PageRowNum = 20;
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.dataOptions = response.BODY.FIEDATA
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      //前端分页
      handleInCurrentChange(val) {
        this.currpage = val;
        this.actQuery();
        // console.log(`当前页: ${val}`);
      },
      handleInSizeChange(val) {
        this.pageSize = val;
        this.actQuery();
      },
      resetForm(formName) { //重置查询表单
        this.$refs["" + formName].resetFields();
        // this.checkedIds = [];
      },
      update(row) {
        this.editOptions.title = '修改活动类型'
        this.editOptions.isShow = true
        this.editOptions.row = row
      },
      addAct() {
        this.editOptions.title = '新增活动类型'
        this.editOptions.isShow = true
        this.editOptions.row = {}
      },
      // 活动查询
      async actQuery() {
        let resBody = new Query()
        resBody.data.ACTIVITYTYPE = this.form.actId
        resBody.data.ACTIVITYTYPENAME = this.form.actName
        resBody.data.ACTIVITYCATEG = this.form.actType
        resBody.data.CurrPage = (this.form.actId == '' && this.form.actName == '') ? this.currpage : 1;
        resBody.data.PageRowNum = this.pageSize;
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.tableData = response.BODY.RSPSTRUCT
          this.tableTotal = response.BODY.TotalRowNum;
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      // 删除活动
      delAct(row) {
        this.$confirm('此操作将删除该活动类型, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          let resBody = new Del()
          resBody.data.ACTIVITYTYPE = row.ACTIVITYTYPE
          let response = await request(resBody);
          if (response.SYS_HEAD.ReturnCode == "000000") {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.actQuery()
          } else {
            this.$message({
              type: "error",
              message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
            })
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        })
      },
    },
  }

</script>

<style lang="scss" scoped>
  .calendarColor {
    width: 30%;
    height: 20px;
    margin: auto;

  }

</style>
